using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.SystemServices.Dtos.Department;
using IM.Easy.Core.Attributes;
using MiniExcelLibs;
using System.Net.Mime;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// SysDepartmentController 控制器
    /// </summary>
    [Route("system/dept")]
    public class DepartmentController : ControllerBase
    {
        private readonly IDepartmentService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public DepartmentController(IDepartmentService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有SysDepartment对象
        /// </summary>
        /// <returns>返回SysDepartment列表</returns>
        [HttpGet]
        [Api(Name = "获取组织列表", Perm = "dept:list")]
        public PageResponse<DepartmentResponseDto> GetList([FromQuery] SysDepartmentQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 获取组织部门树
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Api(Name = "获取组织机构树", Perm = "dept:tree")]
        public List<DepartmentResponseDto> GetTree([FromQuery] SysDepartmentTreeQueryDto query)
        {
            var trees = _service.GetTree(query);
            var unknowOrg = _service.GetUnknowDepartment();
            trees.Insert(0, unknowOrg);
            return trees;
        }

        /// <summary>
        /// 根据ID获取SysDepartment对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysDepartment对象</returns>
        [HttpGet("{id}")]
        [Api(Name = "获取组织", Perm = "dept:info")]
        public DepartmentResponseDto GetById([FromRoute] long id)
        {
            var result = _service.GetOrg(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysDepartment对象。");
        }

        /// <summary>
        /// 创建新的SysDepartment对象
        /// </summary>
        /// <param name="entity">要创建的SysDepartment对象</param>
        /// <returns>返回创建后的SysDepartment对象</returns>
        [HttpPost]
        [Api(Name = "添加组织", Perm = "dept:add")]
        public DepartmentResponseDto Create([FromBody] SysDepartmentCreateDto entity)
        {
            var response = _service.Create(entity);
            return response;
        }

        /// <summary>
        /// 更新现有的SysDepartment对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysDepartment对象</param>
        [HttpPut]
        [Api(Name = "修改组织", Perm = "dept:put")]
        public void Update([FromBody] SysDepartmentCreateDto entity)
        {
            _service.Update(entity);
        }

        /// <summary>
        /// 单个删除SysDepartment对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        [HttpDelete("{id}")]
        [Api(Name = "删除组织", Perm = "dept:del")]
        public void Delete([FromRoute] long id)
        {
            _service.DeleteDept(id);
        }

        /// <summary>
        /// 更新组织类型
        /// </summary>
        [HttpPut("setType")]
        [Api(Name = "组织类型变更", Perm = "dept:type:put")]
        public void SetOrgType([FromBody] SysOrgTypeDto orgType)
        {
            _service.SetOrgType(orgType);
        }

        /// <summary>
        /// 导出组织机构
        /// </summary>
        /// <param name="query"></param>
        [Api(Name = "导出组织机构", Perm = "dept:export")]
        [HttpGet("export")]
        public FileStreamResult Export([FromQuery] SysDepartmentQueryDto query)
        {
            var data = _service.GetExportOrg(query);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, MediaTypeNames.Application.Octet)
            {
                FileDownloadName = "组织机构.xlsx"
            };
        }
    }
}
